home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / windows5 / xwinc100.zip / CONTRIB-.00 / CONTRIB- / contrib / examples / Xaw / xscroll.c < prev    next >
C/C++ Source or Header  |  1991-01-22  |  5KB  |  169 lines

  1. /*
  2.  * xscroll.c
  3.  *
  4.  * This an example of how to use the Scrollbar widget.
  5.  *
  6.  * This example also shows how to set a float resource in an argument list.
  7.  *
  8.  * User events handled through callback routines.
  9.  *
  10.  * November 14, 1989 - Chris D. Peterson 
  11.  */
  12.  
  13. /*
  14.  * $XConsortium: xscroll.c,v 1.15 91/01/22 19:44:30 gildea Exp $
  15.  *
  16.  * Copyright 1989 Massachusetts Institute of Technology
  17.  *
  18.  * Permission to use, copy, modify, distribute, and sell this software and its
  19.  * documentation for any purpose is hereby granted without fee, provided that
  20.  * the above copyright notice appear in all copies and that both that
  21.  * copyright notice and this permission notice appear in supporting
  22.  * documentation, and that the name of M.I.T. not be used in advertising or
  23.  * publicity pertaining to distribution of the software without specific,
  24.  * written prior permission.  M.I.T. makes no representations about the
  25.  * suitability of this software for any purpose.  It is provided "as is"
  26.  * without express or implied warranty.
  27.  *
  28.  * M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
  29.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL M.I.T.
  30.  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  31.  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  32.  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN 
  33.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  34.  */
  35.  
  36. #include <stdio.h>
  37.  
  38. #include <X11/Intrinsic.h>
  39. #include <X11/StringDefs.h>
  40.  
  41. #include <X11/Xaw/Scrollbar.h>
  42. #include <X11/Xaw/Cardinals.h>    
  43.  
  44. static void Scrolled(), Jumped();
  45. static void Syntax();
  46.  
  47. static XrmOptionDescRec options[] = {
  48. {"-horiz",    "scrollbar.orientation", XrmoptionNoArg,  "horizontal"}
  49. };
  50.  
  51. void
  52. main(argc, argv)
  53. int argc;
  54. char **argv;
  55. {
  56.     XtAppContext app_con;
  57.     Widget toplevel, scrollbar;
  58.     XtArgVal * l_top;
  59.     float top;
  60.     Arg args[2];
  61.     Cardinal num_args;
  62.  
  63.     toplevel = XtAppInitialize(&app_con, "Xscroll", options, XtNumber(options),
  64.                    &argc, argv, NULL, NULL, ZERO);
  65.  
  66.     if (argc != 1)        
  67.     Syntax(app_con, argv[0]);
  68.  
  69.     
  70.     /*
  71.      * Setting a float resource in an argument list.
  72.      */
  73.  
  74.     top = 0.5;
  75.     num_args = 0;
  76.     if (sizeof(float) > sizeof(XtArgVal)) {
  77.     /*
  78.      * If a float is larger than an XtArgVal then pass this 
  79.      * resource value by reference.
  80.      */
  81.     XtSetArg(args[num_args], XtNtopOfThumb, &top); num_args++;
  82.     }
  83.     else {
  84.     /*
  85.      * Convince C not to perform an automatic conversion, which
  86.      * would truncate 0.5 to 0. 
  87.      */
  88.     l_top = (XtArgVal *) ⊤
  89.     XtSetArg(args[num_args], XtNtopOfThumb, *l_top); num_args++;
  90.     }
  91.  
  92.     /*
  93.      * Setting other resources is much simpler.
  94.      */
  95.  
  96.     XtSetArg(args[num_args], XtNlength, 200); num_args++;
  97.  
  98.     scrollbar = XtCreateManagedWidget("scrollbar", scrollbarWidgetClass,
  99.                       toplevel, args, num_args);
  100.  
  101.     /*
  102.      * Add a callback routine to the Scrollbar widget that will inform
  103.      * us when the scrollbar has jumped, or scrolled.
  104.      */
  105.  
  106.     XtAddCallback(scrollbar, XtNjumpProc, Jumped, NULL);
  107.     XtAddCallback(scrollbar, XtNscrollProc, Scrolled, NULL);
  108.  
  109.     XtRealizeWidget(toplevel);
  110.     XtAppMainLoop(app_con);
  111. }
  112.  
  113. /*    Function Name: Scrolled
  114.  *    Description: This function prints a message to stdout.
  115.  *    Arguments: w - ** UNUSED **
  116.  *                 client_data - ** UNUSED **
  117.  *                 call_data - the number of pixels scrolled.
  118.  *    Returns: none
  119.  */
  120.  
  121. /* ARGSUSED */
  122. static void
  123. Scrolled(w, closure, call_data)
  124. Widget w;
  125. XtPointer closure, call_data;
  126. {
  127.     fprintf(stdout,
  128.        "scrolled by %d pixels.\n", (int) call_data );
  129. }
  130.  
  131. /*    Function Name: Jumped
  132.  *    Description: This function prints a message to stdout.
  133.  *    Arguments: w - ** UNUSED **
  134.  *                 client_data - ** UNUSED **
  135.  *                 call_data - a pointer to a float containing 
  136.  *                               the location selected.
  137.  *    Returns: none 
  138.  */
  139.  
  140. /* ARGSUSED */
  141. static void
  142. Jumped(w, closure, call_data)
  143. Widget w;
  144. XtPointer closure, call_data;
  145. {
  146.     float top = *((float *) call_data);
  147.  
  148.     fprintf(stdout,
  149.         "thumbed at %4.2f%% the height of the scrollbar\n", top * 100 );
  150. }
  151.  
  152. /*    Function Name: Syntax
  153.  *    Description: Prints a the calling syntax for this function to stdout.
  154.  *    Arguments: app_con - the application context.
  155.  *                 call - the name of the application.
  156.  *    Returns: none - exits tho.
  157.  */
  158.  
  159. static void 
  160. Syntax(app_con, call)
  161. XtAppContext app_con;
  162. char *call;
  163. {
  164.     XtDestroyApplicationContext(app_con);
  165.     fprintf( stderr, "Usage: %s [-horiz]\n", call);
  166.     exit(1);
  167. }
  168.  
  169.